<template>
  <div>
    <van-form @submit="onSubmit">
      <!-- <van-field
        v-model="user.userName"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' },
          {
            validator: (value) => {
              if (value === 'a') {
                return true
              } else {
                return false
              }
            },
            message: '别瞎想小心打你'
          }
          ]"
      /> -->
      <van-field
        v-model="user.userName"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' }
          ]"
      />
      <van-field
        v-model="user.pwd"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">提交</van-button>
      </div>
  </van-form>
  </div>
</template>

<script>
import { Toast } from 'vant'
export default {
  data () {
    return {
      user: {
        userName: '',
        pwd: ''
      }
    }
  },
  methods: {
    onSubmit () {
      // 提交表单事件
      setTimeout(() => {
        const authToken = 'feihfioge394t598trhfcis'
        const userInfo = {
          nickName: '小甜甜',
          avatar: 'http://himg.bdimg.com/sys/portrait/item/d1846c697869616e676d616769638c27.jpg'
        }
        localStorage.setItem('auth_token', authToken)
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
        Toast('登录成功')
        if (this.$route.query.from) {
          this.$router.replace(this.$route.query.from)
        } else {
          this.$router.replace('/home')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
